﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NRemedy;
using System.Configuration;
using NRemedy.Linq;
using Oracle.DataAccess.Client;

namespace Syncer
{
    public class SLAImport
    {
        public Dictionary<string, Int32> _ComDic { get; set; }

        public Dictionary<string, Int32> _BsDic { get; set; }

        public void Execute(Param param)
        {
            Log.Info("start SLA import.");
            //get application config
            string arserver = ConfigurationManager.AppSettings["ARServer"].ToString();
            string user = ConfigurationManager.AppSettings["User"].ToString();
            string pwd = ConfigurationManager.AppSettings["Pwd"].ToString();

            _ComDic = param.ComDic;
            _BsDic = param.BsDic;

            using (ARLoginContext context = new ARLoginContext(arserver, user, pwd))
            {
                ARSet<SLMMeasurement> incidents = new ARSet<SLMMeasurement>(context, new ARServerDefaultFactory());
                var query = from i in incidents
                            where i.Modifed_Date >= param.StartTime && i.Modifed_Date <= param.EndTime
                            select i;

                using (OracleDataAccess oda = new OracleDataAccess())
                {
                    foreach (var slm in query)
                    {
                        string sql = "select ID from SLA where ID=:requestid";
                        OracleParameter[] _param = new OracleParameter[] { 
                            new OracleParameter("requestid",slm.Request_ID)
                        };
                        object requestid = oda.ExecuteScalar(sql, _param);
                        //if not exsit,should be insert
                        if (requestid == null)
                        {
                            try
                            {
                                InsertSLA(slm, oda);
                                Log.Info("Insert " + slm.Request_ID + " OK.");
                            }
                            catch (Exception ex)
                            {
                                Log.Exception("Insert " + slm.Request_ID + " failed.", ex);
                            }
                        }
                        else
                        {
                            try
                            {
                                //update incident
                                UpdateSLA(slm, oda);
                                Log.Info("Update " + slm.Request_ID + " OK.");
                            }
                            catch (Exception ex)
                            {
                                Log.Exception("Update " + slm.Request_ID + " failed.", ex);
                            }
                        }
                    }
                }
            }
            Log.Info("SLA import end.");
        }


        private void InsertSLA(SLMMeasurement sla, OracleDataAccess oda)
        {
            string insert_sql = @"insert into SLA(
                                    ID,
                                    ApplicationID,
                                    ApplicationType,
                                    SLAId,
                                    SLATitle,
                                    SLAGoalTime,
                                    SLACost,
                                    SLAStartTime,
                                    SLAEndTime,
                                    SLAStatus,
                                    SLACategoryChar
                                    ) 
                            values (
                                        :request_id,
                                        :applicationId,
                                        :datasource,
                                        :slmId,
                                        :svttitle,
                                        :goaltime,
                                        :cost,
                                        :overallstarttime,
                                        :overallendtime,
                                        :status,
                                        :goalcategorychar
                                    )";
            OracleParameter[] param = new OracleParameter[] { 
                            new OracleParameter("request_id",sla.Request_ID),
                            new OracleParameter("applicationId",sla.ApplicationUserFriendlyID),
                            new OracleParameter("datasource",sla.DataSourceAppForm),
                            new OracleParameter("slmId",sla.SLMId),
                            new OracleParameter("svttitle",sla.SVTTitle),
                            new OracleParameter("goaltime",(int)sla.GoalSchedGoalTime),
                            new OracleParameter("cost",sla.GoalSchedCost == null ? 0 : (int)sla.GoalSchedCost.Value),
                            new OracleParameter("overallstarttime",sla.OverallStartTime),
                            new OracleParameter("overallendtime",sla.OverallStopTime),
                            new OracleParameter("status",(int)sla.MeasurementStatus),
                            new OracleParameter("goalcategorychar",sla.GoalCategoryChar)
                        };

            oda.ExecuteCommand(insert_sql, param);
        }

        private void UpdateSLA(SLMMeasurement sla, OracleDataAccess oda)
        {
            string update_sql = @"update SLA set
                                   ApplicationID=	:applicationId,
                                    ApplicationType=	:datasource,
                                    SLAId=	:slmId,
                                    SLATitle=	:svttitle,
                                    SLAGoalTime=	:goaltime,
                                    SLACost=	:cost,
                                    SLAStartTime=	:overallstarttime,
                                    SLAEndTime=	:overallendtime,
                                    SLAStatus=	:status,
                                    SLACategoryChar= :goalcategorychar
                                    where ID=:request_id
                                    ";
            OracleParameter[] param = new OracleParameter[] { 
                            new OracleParameter("request_id",sla.Request_ID),
                            new OracleParameter("applicationId",sla.ApplicationUserFriendlyID),
                            new OracleParameter("datasource",sla.DataSourceAppForm),
                            new OracleParameter("slmId",sla.SLMId),
                            new OracleParameter("svttitle",sla.SVTTitle),
                            new OracleParameter("goaltime",(int)sla.GoalSchedGoalTime),
                            new OracleParameter("cost",sla.GoalSchedCost == null ? 0 : (int)sla.GoalSchedCost.Value),
                            new OracleParameter("overallstarttime",sla.OverallStartTime),
                            new OracleParameter("overallendtime",sla.OverallStopTime),
                            new OracleParameter("status",(int)sla.MeasurementStatus),
                            new OracleParameter("goalcategorychar",sla.GoalCategoryChar)
                        };


            oda.ExecuteCommand(update_sql, param);
        }
    }
}
